/**
 * Created by fdg282 on 2017/8/4.
 */
$(function(){
    var banners = new Swiper('#banner', {
        nextButton: '.banner-next',
        prevButton: '.banner-prev',
        spaceBetween:10,
        direction: 'horizontal',
        pagination : '.swiper-pagination',
        autoplay: 4000,
        speed:3000,
        effect : 'fade',
        loop:true,
        paginationClickable :true

    });
    if(navigator.userAgent.indexOf("MSIE 9.0")>0) {
        var bannersIE = new Swiper('#banner', {
            nextButton: '.banner-next',
            prevButton: '.banner-prev',
            spaceBetween:10,
            direction: 'horizontal',
            pagination : '.swiper-pagination',
            autoplay: 4000,
            speed:3000,
            loop:true,
            paginationClickable :true
        });
    }

    var mySwiper = new Swiper('#brands_swiper', {
        nextButton: '.brands-button-next',
        prevButton: '.brands-button-prev',
        slidesPerView:5,
        paginationClickable: true,
        spaceBetween: 20,
        loop : true
    });

    var hotList = new Swiper('#hot-list', {
        nextButton: '.hot-button-next',
        prevButton: '.hot-button-prev',
        spaceBetween: 10,
        direction: 'horizontal',
        loop:true,
        speed:3000,
        lazyLoading : true
    });
    //hot商品动效
    $(".hot-item,.brands_swiper .item").each(function(){
        $(this).hover(function(){
            $(this).removeClass("hot-itemShow")
        },function(){
            $(this).addClass("hot-itemShow")
        })
    });
    // 通过微信扫码绑定弹框
    if(is_binding == 0) {
        layer.open({
            type: 1,
            title: false,
            closeBtn:1,
            shadeClose: false,
            skin: '.layer_webchat',
            content: $('.layer_webchat'),
            cancel: function(){
                //回调删除登录缓存
                $.ajax({
                    type: "post",
                    data: '',
                    url: "/login/logout",
                    beforeSend: function () {
                        // 禁用按钮防止重复提交
                        layer.load(1);
                        //此处演示关闭
                       /* setTimeout(function(){
                            layer.closeAll('loading');
                        }, 2000);*/
                    },
                    success: function (data) {
                        location.reload();
                    },
                    complete: function () {
                        layer.closeAll('loading');
                    },
                    error: function (data) {
                        console.info("error: " + data.responseText);
                    }
                });
            }
        });
        $('html').addClass('noscroll');
        var reg = /^1(0|1|2|3|4|5|6|7|8|9)\d{9}$/;
        var imgCode = $(".js_img_code");
        var phoneCode = $(".js_phone_code");
        var phoneVal = $(".js_phone");
        var imgCodeInfo = $(".img_verification .info");
        var logUseInfo = $(".form_phone_group .info");  //手机号码验证提示信息
        var phoneCodeInfo = $(".phone_verification .info"); //手机短信验证码提示信息
        var tipshtml = "<div class='info_msg'>绑定成功<br/>窗口将在<span id='time'></span>后关闭 </div>";
        var img = $(".js_img_code").val();
        var phone = $(".js_phone_code").val();
        var phoneC = $(".js_phone").val();
        var info = "<span class='info'></span>";
        $(".form_phone_group,.img_verification,.phone_verification").append(info);
        $(".layer_webchat").removeClass("hide");
        /*$(".layer_webchat input").keyup(function () {
            if (img != "" && phone != "" && phoneC != "") {
                $(".j-v-btn").addClass("j-v-btn-active")
            }
        });*/
        $(".form_input_group.info").empty();
        $(".js_img_code").keyup(function(){
            var imgCodeVal = $(".img_verification_value").val();
            if(imgCodeVal.length >= 4){
                $(".img_verification .info").html('');
                $(".tipinfo").html('');
                $(".js_img_code").addClass("infowaring");
            }else{
                $(".img_verification .info").html('图形验证码格式错误,请重新输入！').addClass("infoerror");
                $(".js_img_code").removeClass("infowaring");
            }
        });

        $(".js_phone").keyup(function () {
            var userphone = $(this).val();
            if (userphone <= 0) {
                $(".form_phone_group .info").html("手机号不能为空！").addClass("infoerror");
                $(".js_phone").removeClass("infowaring");
            } else if (!reg.test(userphone)) {
                $(".form_phone_group .info").html("请输入正确的11位手机号！").addClass("infoerror");
                $(".js_phone").removeClass("infowaring");
            } else {
                $(".phoneV_btn").attr("disabled",false);
                $(".form_phone_group .info").html('');
                $(".tipinfo").html('');
                $(".js_phone").addClass("infowaring");
            }
        });

        $(".js_phone_code").keyup(function(){
            var phoneCodeVal = $(this).val();

            if(phoneCodeVal.length>=6){
                $('.j-v-btn').css('background','#333333');
                $(".phone_verification .info").html('');
                $(".tipinfo").html('');
                $(".js_phone_code").addClass("infowaring");
            }else{
                $(".phone_verification .info").html("手机验证码格式错误,请重新输入").addClass("infoerror");
                $(".js_phone_code").removeClass("infowaring");
                $('.j-v-btn').css('background','#cccccc');
            }
        });

        $(".j-v-btn").click(function () {

            var phone = $(".js_phone").val();
            var phoneC = $(".js_phone_code").val();
            if(!reg.test(phone)){
                $(".form_phone_group .info").html("请输入正确的11位手机号").addClass("infoerror");
                $(".js_phone").removeClass("infowaring");
                return false;
            }

            if(phoneC.length != 6){
                $(".phone_verification .info").html("手机验证码格式错误,请重新输入").addClass("infoerror");
                $(".js_phone_code").removeClass("infowaring");
                return false;
            }
            $(this).attr("disabled",true);
            //
            $.ajax({
                type: "post",
                data: $('#validate-form').serialize(),
                url: $('#validate-form').attr('action'),
                dataType:'json',
                beforeSend: function () {
                    // 禁用按钮防止重复提交
                    layer.load(1);
                    //此处演示关闭
                    /* setTimeout(function(){
                     layer.closeAll('loading');
                     }, 2000);*/
                },
                success: function (data) {
                    console.log(data);
                    if(data.code == 200){
                        $(".layer_webchat_form").find(".form_input_group").hide();
                        if ($(".layer_webchat").find(".info_msg").length <= 0) {
                            $(".layer_webchat").append(tipshtml);
                            var time = 5;
                            $("#time").text(time+"S");
                            var tim = setInterval(function(){
                                $("#time").text(time+"S");
                                time--;
                                if(time==0){
                                    clearInterval(tim);
                                    window.layer.close();
                                    window.location.reload();
                                }
                            },1000);
                        }
                    }
                    if(data.code == 502){
                        window.location.href = data.url;
                    }
                    if(data.code == 500){
                        $(".j-v-btn").attr("disabled",false);
                        $(".tipinfo").text(data.val);
                    }
                },
                complete: function () {
                    layer.closeAll('loading');
                },
                error: function (data) {
                    console.info("error: " + data.responseText);
                }
            });

        });

        //倒计时发送短信
        $(".phoneV_btn").click(function(){
            $(this).attr("disabled",true).addClass("btn-active");

            if($(this).parents().find(".js_img_code").val()==''){
                $(this).parents().find(".js_img_code").addClass("inputerror");
                $(this).parents().find(".js_img_code").siblings(".info").html("验证码不能为空").addClass("infoerror");
                $(this).attr("disabled",false).removeClass("btn-active");
                return false;
            }

            if($(this).parents().find(".js_phone").val() == ''){
                $(this).parents().find(".js_phone").addClass("inputerror");
                $(this).parents().find(".js_phone").siblings(".info").html("手机号不能为空").addClass("infoerror");
                $(this).attr("disabled",false).removeClass("btn-active");
                return false;
            }

            //发送短信
            var send_mobile = $(this).parents().find(".js_phone").val();

            var verifyCode = $(this).parents().find(".js_img_code").val();

            if(!reg.test(send_mobile)){
                $(this).parents().find(".js_img_code").addClass("inputerror");
                $(this).parents().find(".js_img_code").siblings(".info").html('请输入正确的手机号').addClass("infoerror");
                $(this).attr("disabled",false).removeClass("btn-active");
                return false;
            }

            if(verifyCode.length < 4){
                $(".phone_verification .info").html("验证码格式错误,请重新输入").addClass("infoerror");
                $(this).removeClass("infowaring");
                $(this).attr("disabled",false).removeClass("btn-active");
                return false;
            }

            $.post('/login/mobile-code',{mobile:send_mobile,verifyCode:verifyCode},success,'json');
            return false;
            function success(data){
                var that = $('.phoneV_btn');
                if(data.code == 200){
                    var time = 60;
                    //连接接口发送短信
                    var tim = setInterval(function(){
                        that.text(time+"S后重新发送");
                        time--;
                        if(time==0){
                            clearInterval(tim);
                            that.text("获取手机验证码").attr("disabled",false).removeClass("btn-active");
                        }
                    },1000);
                }else{
                    $('#captchaform-verifycode-image').trigger('click');
                    $(".tipinfo").text(data.val);
                    $(that).attr("disabled",false).removeClass("btn-active");
                }
            }
        });

        function inputsure(){
            var phoneInpterror = function(wrap,inp,inpInfo){
                wrap.find(inp).focus(function(){
                    $Val = wrap.find(inp).val();
                    if ($Val.length <= 0) {
                        wrap.find(inp).addClass("inputerror");
                        wrap.find(inpInfo).html("此项不能为空").addClass("infoerror");
                    }
                });
            };

            //手机验证登录
            phoneInpterror($('#form_2'),$(".js_phone"),$(".form_phone_group .info"));
            phoneInpterror($('#form_2'),$(".js_img_code"),$(".img_verification .info"));
            phoneInpterror($('#form_2'),$(".js_phone_code"),$(".phone_verification .info"));
        }

        inputsure();
        
    }
});